package game.module.mj.processor;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubRoomManager;
import game.module.hall.logic.CheatManager;
import game.module.mj.bean.MjRoom;
import game.module.mj.bean.MjRoomCfg;
import game.module.mj.dao.MjRoomTemplateCache;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMJCreateRoom;
import lua.LuaMessageMj.S2CMJCreateRoom;
import lion.common.MsgCodeAnn;

@MsgCodeAnn(msgcode = C2SMJCreateRoom.id, accessLimit = 500)
public class MjCreateRoomProcessor extends PlayingRoleMsgProcessor {
	private static Logger logger = LoggerFactory.getLogger(MjCreateRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SMJCreateRoom reqMsg = C2SMJCreateRoom.parse(request);
		int round_count = reqMsg.round_count;
		byte dapai = reqMsg.dapai;// 有无大牌
		byte typehu = reqMsg.typehu;// 放胡单配和放胡通赔
		byte prevent_cheat = reqMsg.prevent_cheat;// 防作弊
		byte gold_coin = reqMsg.gold_coin;// 金豆模式,参数下标
		int gold_rate = reqMsg.gold_rate; // 自定义
		byte cost_type = reqMsg.cost_type;
		int playerId = hero.getId();
		logger.info("mj create room,playeyId={},round_count={},zhuajia_type={}", playerId, round_count);
		// 是否在房间中
		if (!MjRoomTemplateCache.getInstance().containsConfigIndex(round_count)) {
			S2ErrorCode respMsg = new S2ErrorCode(101);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		boolean isInRoom = MjDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		MjRoomCfg mjRoomCfg = MjRoomTemplateCache.getInstance().getTemplate(round_count);
		// is enough
		int costFangka = mjRoomCfg.getCost_fangka();
		if (hero.getPlayerBean().getFang_ka() < costFangka) {
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// do
		MjRoom mjRoom = MjLogic.getInstance().createMjRoom(playerId, mjRoomCfg.getRound(), dapai, typehu, prevent_cheat, gold_rate, cost_type);
		CheatManager.getInstance().openCheatComm(prevent_cheat, mjRoom.getIps(), hero);
		ClubRoomManager.getInstance().openClubRoom(hero, mjRoom);

		S2CMJCreateRoom respMsg = new S2CMJCreateRoom();

		respMsg.room_id = mjRoom.getUuid();
		respMsg.round_count = mjRoom.getRoundCount();
		respMsg.dapai = dapai;
		respMsg.typehu = typehu;
		respMsg.prevent_cheat = prevent_cheat;
		respMsg.cost_type = cost_type;
		// ret
		hero.writeAndFlush(respMsg.build(hero.alloc()));

	}

	public String getIpAddr(String ipAddrRaw) {
		String retStr = ipAddrRaw;
		int fenIndex = ipAddrRaw.indexOf(":");
		if (fenIndex >= 0) {
			retStr = retStr.substring(0, fenIndex);
		}
		if (retStr.startsWith("/")) {
			retStr = retStr.substring(1);
		}
		return retStr;
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
